package com.test.service.Impl;

import com.test.dao.UserMapper;
import com.test.entity.AuthUser;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserAuthService implements UserDetailsService {
   @Resource
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        AuthUser user=userMapper.getUserByLogin(s);
        if(user==null){
            throw new UsernameNotFoundException("登录失败，用户名或密码错误");
        }
        return User.withUsername(s)
                .password(user.getPassword())
                .roles(user.getRole())
                .build();
    }
}
